<template>
    <van-sticky z-index="100">
        <van-nav-bar
            title="编辑收货地址"
            left-text="返回"
            left-arrow
            @click-left="onBack"
        />
    </van-sticky>

    <van-address-edit
        :address-info="info"
        :area-list="areaList"
        show-delete
        show-set-default
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
        @delete="onDelete"
    />

</template>
<script setup>
import { getCurrentInstance, onMounted, ref, reactive } from 'vue';
import { areaList } from '@vant/area-data';

// 定义变量
const { proxy } = getCurrentInstance();
const business = reactive(proxy.$cookies.get('business') ? proxy.$cookies.get('business') : {});
const id = ref(proxy.$route.query.id ? proxy.$route.query.id : 0);
const info = ref({});

onMounted(() => {
    getInfo();
});

const getInfo = async () => {
    let data = {
        busid:business.id,
        id:id.value
    }

    let result = await proxy.$api.AddressInfo(data);

    if(result.code === 0)
    {
        proxy.$showNotify({
            type:'warning',
            message:result.msg,
            duration:1500,
            onClose:() => {
                proxy.$router.go(-1);
            }
        });

        return;
    }

    let address = result.data;

    info.value = {
        name:address.consignee,
        tel:address.mobile,
        areaCode:address.district ? address.district : (address.city ? address.city : address.province),
        addressDetail:address.address,
        isDefault:address.status == 1 ? true : false
    }
}

const onSave = async (info) => {
    // 组装数据
    let data = {
        id:id.value,
        busid:business.id,
        consignee:info.name,
        mobile:info.tel,
        code:info.areaCode,
        address:info.addressDetail,
        status:info.isDefault === true ? 1 : 0
    }
    // return data;
    // 发起请求
    let result = await proxy.$api.AddressEdit(data);

    if(result.code === 1)
    {
        proxy.$showNotify({
            type:'success',
            message:result.msg,
            duration:1500,
            onClose:() => {
                proxy.$router.go(-1);
            }
        });

        return;
    }else{
        proxy.$showNotify({
            type:'warning',
            message:result.msg,
            duration:1500
        });

        return;
    }
}

const onDelete = (info) => {

    proxy.$showConfirmDialog({
        title:'删除',
        message:'是否确认删除该收货地址？'
    }).then(async (res) => {
        let data = {
            busid:business.id,
            id:id.value
        }

        let result = await proxy.$api.AddressDel(data);

        if(result.code === 1)
        {
            proxy.$showNotify({
                type:'success',
                message:result.msg,
                duration:1500,
                onClose:() => {
                    proxy.$router.go(-1);
                }
            });

            return;
        }else{
            proxy.$showNotify({
                type:'warning',
                message:result.msg,
                duration:1500
            });

            return;
        }
    }).catch((err) => {
        console.log('取消操作')
    });

}

const onBack = () => {
    proxy.$router.go(-1);
    return;
}
</script>